package com.miao.aop02;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
@Aspect
public class PicUploadAop {


    //定义切点
    @Pointcut("execution( * com.miao.aop02.UserController.*(..))")
    public void pointCut(){
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) throws IllegalAccessException {
        //获取到全部参数
        Object[] values = joinPoint.getArgs();
        //取参数对象
        Object reqObject = values[0];

        //取出参数对象中的 全部属性值字段
        Field[] fields = reqObject.getClass().getDeclaredFields();

        for (Field field : fields) {
            boolean hasBase64PicField = field.isAnnotationPresent(Base64Pic.class);
            if (hasBase64PicField) {
                //获取到注解
                Base64Pic annotation = field.getAnnotation(Base64Pic.class);
                //判断注解值
                if (annotation.value()) {
                    //进行针对某个属性的操作
                    field.setAccessible(true);

                    //取对应字段的值
                    String base64Str = (String) field.get(reqObject);
                    //上传图片
                    //重新给字段赋值
                    String urlStr = "http:xxxxxxxx";
                    field.set(reqObject, urlStr);
                }
            }
        }
    }


}
